package logger

import (
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"io"
	"log"
	"os"
	"time"
)

const (
	rootPath = "./logs"
)

var (
	Info, Warning, Error *log.Logger
)

func Configuration(name string) {
	infoFileName := name + "-info"
	warningFileName := name + "-warning"
	errorFileName := name + "-error"
	Info = log.New(newWriter(infoFileName), "Info ", log.Ldate|log.Ltime|log.Lshortfile)
	Warning = log.New(newWriter(warningFileName), "Warning ", log.Ldate|log.Ltime|log.Lshortfile)
	Error = log.New(newWriter(errorFileName), "Error ", log.Ldate|log.Ltime|log.Lshortfile)
}
func newWriter(name string) io.Writer {
	fileName := rootPath + "/" + name + ".%Y-%m-%d.log"
	writer, _ := rotatelogs.New(
		//日志路径
		fileName,
		//link当前日志文件
		rotatelogs.WithLinkName(rootPath+"/"+name+".log"),
		//自动清除90天以前日志
		rotatelogs.WithMaxAge(time.Duration(60)*time.Hour),
		//每天生成一个文件
		rotatelogs.WithRotationTime(time.Duration(24)*time.Hour),
		//单个文件最大512MB
		rotatelogs.WithRotationSize(512*1024*1024),
	)
	return io.MultiWriter(os.Stdout, writer)
}
